﻿<template>
	<div class="deiviceTemplateTrigger-container">
		<el-dialog v-model="isShowDialog" :width="800" draggable="" :close-on-click-modal="false">
			<template #header>
				<div style="color: #fff">
					<!--<el-icon size="16" style="margin-right: 3px; display: inline; vertical-align: middle"> <ele-Edit /> </el-icon>-->
					<span>{{ props.title }}</span>
				</div>
			</template>
			<el-form :model="ruleForm" ref="ruleFormRef" label-width="auto" :rules="rules">
				<el-row :gutter="35">
					<el-form-item v-show="false">
						<el-input v-model="ruleForm.id" />
					</el-form-item>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="触发器名" prop="triggerName">
							<el-input v-model="ruleForm.triggerName" placeholder="请输入触发器名" maxlength="100" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="从机变量" >
							<el-input v-model="ruleForm.pointName" placeholder="请输入触发器名" maxlength="100"  disabled="true" />
						</el-form-item>
						
					</el-col>
					
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="是否预警" prop="status">
							<el-switch v-model="ruleForm.status" active-text="是" inactive-text="否" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="报警抓拍" prop="capture">
							<el-switch v-model="ruleForm.capture" active-text="是" inactive-text="否" />
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="触发条件" prop="triggerCondition">
							<el-select clearable v-model="ruleForm.triggerCondition" placeholder="请选择触发条件">
								<el-option v-for="(item,index) in dl('TriggerCondition')" :key="index" :value="Number(item.value)" :label="`${item.name} (${item.code}) [${item.value}]`"></el-option>
							</el-select>
						</el-form-item>
					
					</el-col>
					<el-col :xs="24" :sm="6" :md="6" :lg="6" :xl="6" class="mb20">
						<el-form-item label="A值" prop="min">
							<el-input v-model="ruleForm.min" placeholder="A值"   clearable />
						</el-form-item>
					
					</el-col>
					<el-col :xs="24" :sm="6" :md="6" :lg="6" :xl="6" class="mb10">
						<el-form-item label="B值" prop="max">
							<el-input v-model="ruleForm.max" placeholder="B值"   clearable />
						</el-form-item>
					</el-col>
					
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="报警推送内容" prop="customContentAlarm">
							<el-input v-model="ruleForm.customContentAlarm" placeholder="请输入报警推送内容" type="textarea" maxlength="500" show-word-limit clearable />
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="报警恢复推送内容" prop="customContentNormal">
							<el-input v-model="ruleForm.customContentNormal" placeholder="请输入报警恢复推送内容" type="textarea" maxlength="500" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="是否联动" prop="linkageControlStatus">
							<el-switch v-model="ruleForm.linkageControlStatus" active-text="是" inactive-text="否" />
						</el-form-item>
					</el-col>
					
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="联动类型" prop="controlType">
							<el-select clearable v-model="ruleForm.controlType" placeholder="请选择联动类型">
								<el-option v-for="(item,index) in dl('ControlType')" :key="index" :value="Number(item.value)" :label="`${item.name} (${item.code}) [${item.value}]`"></el-option>
								
							</el-select>
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="发送类型" prop="linkageControlSendType">
							<el-select clearable v-model="ruleForm.linkageControlSendType" placeholder="请选择发送类型">
								<el-option v-for="(item,index) in dl('LinkageControlSendType')" :key="index" :value="Number(item.value)" :label="`${item.name} (${item.code}) [${item.value}]`"></el-option>
								
							</el-select>
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="联动的变量" prop="controlTemplatePointId">
							<el-select clearable filterable v-model="ruleForm.controlTemplatePointId" placeholder="请选择联动的变量">
								<el-option v-for="(item,index) in deviceSlaveTemplatePointsControlTemplatePointIdDropdownList" :key="index" :value="item.value" :label="item.label" />
								
							</el-select>
							
						</el-form-item>
						
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="下发数据" prop="controlSendData">
							<el-input v-model="ruleForm.controlSendData" placeholder="请输入下发数据" maxlength="255" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="报警死区" prop="deadZone">
							<el-input v-model="ruleForm.deadZone" placeholder="请输入报警死区" maxlength="0" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col>
					<!-- <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="备注" prop="remark">
							<el-input v-model="ruleForm.remark" placeholder="请输入备注" type="textarea" maxlength="1000" show-word-limit clearable />
							
						</el-form-item>
						
					</el-col> -->
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="cancel">取 消</el-button>
					<el-button type="primary" @click="submit">确 定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>
<style lang="scss" scoped>
:deep(.el-select),
:deep(.el-input-number) {
	width: 100%;
}
</style>
<script lang="ts" setup>
	import { ref,onMounted } from "vue";
	import { ElMessage } from "element-plus";
	import type { FormRules } from "element-plus";
	import { getDictDataItem as di, getDictDataList as dl } from '/@/utils/dict-utils';
	import { getDictLabelByVal as dv } from '/@/utils/dict-utils';
  	import { formatDate } from '/@/utils/formatTime';
	import { addDeiviceTemplateTrigger, updateDeiviceTemplateTrigger, detailDeiviceTemplateTrigger,detailByPointId } from "/@/api/Device/deiviceTemplateTrigger";
	import { getDeviceSlaveTemplatePointsTemplatePointIdDropdown } from '/@/api/Device/deiviceTemplateTrigger';
	import { getDeviceSlaveTemplatePointsControlTemplatePointIdDropdown } from '/@/api/Device/deiviceTemplateTrigger';
	import { getAPI } from '/@/utils/axios-utils';
	import { SysEnumApi } from '/@/api-services/api';

	//父级传递来的参数
	var props = defineProps({
		title: {
		type: String,
		default: "",
	},
	});
	//父级传递来的函数，用于回调
	const emit = defineEmits(["reloadTable"]);
	const ruleFormRef = ref();
	const isShowDialog = ref(false);
	const ruleForm = ref<any>({});
	const  templatePointId= ref("");
	const templatePointName = ref("");
	//自行添加其他规则
	const rules = ref<FormRules>({
		triggerName: [{required: true, message: '请输入触发器名！', trigger: 'blur',},],
	});

	// 页面加载时
	onMounted(() => {

	});

	// 打开弹窗
	const openDialog = async (row: any) => {
		// ruleForm.value = JSON.parse(JSON.stringify(row));
		// 改用detail获取最新数据来编辑
		let rowData = JSON.parse(JSON.stringify(row));
		if (rowData.id)
			ruleForm.value = (await detailDeiviceTemplateTrigger(rowData.id)).data.result;
		else
			ruleForm.value = rowData;
		isShowDialog.value = true;
	};

	const openDialogByPointId = async (id: any,pointName:string) => {
		// ruleForm.value = JSON.parse(JSON.stringify(row));
		// 改用detail获取最新数据来编辑
		ruleForm.value = (await detailByPointId(id)).data.result;
		if(ruleForm.value==null){
			ruleForm.value={};
			ruleForm.value.id=0;
			ruleForm.value.templatePointId=id;
			ruleForm.value.pointName=pointName
		}
		
		isShowDialog.value = true;
	};

	// 关闭弹窗
	const closeDialog = () => {
		emit("reloadTable");
		isShowDialog.value = false;
	};

	// 取消
	const cancel = () => {
		isShowDialog.value = false;
	};

	// 提交
	const submit = async () => {
		ruleFormRef.value.validate(async (isValid: boolean, fields?: any) => {
			if (isValid) {
				let values = ruleForm.value;
				values.triggerType=0;
				if (ruleForm.value.id == undefined || ruleForm.value.id == null || ruleForm.value.id == "" || ruleForm.value.id == 0) {
					await addDeiviceTemplateTrigger(values);
				} else {
					await updateDeiviceTemplateTrigger(values);
				}
				closeDialog();
			} else {
				ElMessage({
					message: `表单有${Object.keys(fields).length}处验证失败，请修改后再提交`,
					type: "error",
				});
			}
		});
	};

	const deviceSlaveTemplatePointsTemplatePointIdDropdownList = ref<any>([]); 
	const getDeviceSlaveTemplatePointsTemplatePointIdDropdownList = async () => {
		let list = await getDeviceSlaveTemplatePointsTemplatePointIdDropdown();
		deviceSlaveTemplatePointsTemplatePointIdDropdownList.value = list.data.result ?? [];
	};
	getDeviceSlaveTemplatePointsTemplatePointIdDropdownList();
	
	const deviceSlaveTemplatePointsControlTemplatePointIdDropdownList = ref<any>([]); 
	const getDeviceSlaveTemplatePointsControlTemplatePointIdDropdownList = async () => {
		let list = await getDeviceSlaveTemplatePointsControlTemplatePointIdDropdown();
		deviceSlaveTemplatePointsControlTemplatePointIdDropdownList.value = list.data.result ?? [];
	};
	getDeviceSlaveTemplatePointsControlTemplatePointIdDropdownList();
	





	//将属性或者函数暴露给父组件
	defineExpose({ openDialog ,openDialogByPointId});
</script>




